package com.eccg.shopping.utils;

import java.util.ArrayList;

import com.eccg.shopping.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class StoreAdapter extends BaseAdapter implements SectionIndexer {
	private ArrayList<Store> storesArray;
	private Context context;
	private String a2z = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	public StoreAdapter(Context _context, ArrayList<Store> arr) {
		storesArray = arr;
		context = _context;
	}

	public int getCount() {
		return storesArray.size();
	}

	public Object getItem(int arg0) {
		return storesArray.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int position, View v, ViewGroup parent) {
		LayoutInflater inflate = ((Activity) context).getLayoutInflater();
		View view = (View) inflate.inflate(R.layout.storeselect_item, null);
		LinearLayout header = (LinearLayout) view.findViewById(R.storeselect_item.storeselect_item_section);
		String label = storesArray.get(position).getStoreName();
		String aLabel = storesArray.get(position).getStorePinyin();
		char firstChar = ' ';
		if(aLabel != null && !"".equals(aLabel) && a2z.contains(aLabel.substring(0, 1))){
			firstChar = aLabel.toUpperCase().charAt(0);
		}else{
			firstChar = '#';
		}
		
		if (position == 0) {
			setSection(header, aLabel);
		} else {
			String preLabel = storesArray.get(position - 1).getStorePinyin();
			char preFirstChar = ' ';
			if(preLabel != null && !"".equals(preLabel) && a2z.contains(preLabel.substring(0, 1))){
				preFirstChar = preLabel.toUpperCase().charAt(0);
			}else{
				preFirstChar = '#';
			}
			
			if (firstChar != preFirstChar) {
				setSection(header, aLabel);
			} else {
				header.setVisibility(View.GONE);
			}
		}
		TextView textView = (TextView) view.findViewById(R.storeselect_item.storeselect_itemname_tv);
		textView.setText(label);
		return view;
	}

	private void setSection(LinearLayout header, String label) {
		TextView text = new TextView(context);
		header.setBackgroundColor(0xffaabbcc);
		text.setTextColor(Color.WHITE);
		if(label != null && !"".equals(label) && a2z.contains(label.substring(0, 1).toUpperCase())){
			text.setText("  " + label.substring(0, 1).toUpperCase());
		}else{
			text.setText("  #");
		}
		text.setTextSize(20);
		text.setPadding(5, 0, 0, 0);
		text.setGravity(Gravity.CENTER_VERTICAL);
		header.addView(text);
	}

	public int getPositionForSection(int section) {
		if (section == 100000000) {
			return 0;
		}
		for (int i = 0; i < storesArray.size(); i++) {
			String l = storesArray.get(i).getStorePinyin();
			char firstChar = ' ';
			if(l != null && !"".equals(l) && a2z.contains(l.substring(0, 1))){
				firstChar = l.toUpperCase().charAt(0);
			}else{
				firstChar = '#';
			}
			if (firstChar == section) {
				return i;
			}
		}
		return -1;
	}

	public int getSectionForPosition(int arg0) {
		return 0;
	}

	public Object[] getSections() {
		return null;
	}
}